// free tools for java
Simple
T o o l s
-Assembler
--Script examples
---Simple
---Loops
---Mini arithmetic compiler
  This program generates a simple class that does absolutely nothing besides getting initialized. There are basically two things you need to do to make a class file, specify the hierarchy where the class belongs, and write the methods for the class.
(define my-class (make-class-env))
(jas-class-setsuperclass my-class (make-class-cpe "java/lang/Object"))
(jas-class-setclass my-class (make-class-cpe "out"))
make-class-env creates an instance of a class-env, which is a container object used to used to store information about the contents of a class. As various pieces of a class become available, they are added through the functions that are available to manipulate class-env objects.

make-class-cpe creates an instance of a cpe item. A cpe item is what I call an entry in the constant pool. The constant pool is like a symbol table for class files, and entries in the constant pool can be of several types, one of which is a class-cpe type. Several functions are provided to create cpe items of various types. You should walk through the master function list to see what functions are available.

The jas-class-setclass method sets the class name for the class-env to be the cpe-item. In general, most jas package methods only expect you to provide a cpe, leaving any type checking of the cpe itself to the VM implementation. Correct VM implementations should generate errors if you pass cpe items of the wrong type, but the assembler itself does no such checking.

jas-class-setsuperclass is a function that defines the superclass for the class-env. It too, expects to find a cpe item as its only argument. So the above snippet of code simply creates a new class-env, setting its name to be "out" and defining it to be subclassed from "java/lang/Object".

You can keep creating new cpe items and adding them to the class-env. The package will internally "uniquefy" cpe items, so at the end only one unique copy will be generated in the class file. You can reuse cpe items too, if you wish. It is sometimes just more convenient to create them on the fly instead of carting them with you all the time.

The second step is defining the methods in the class. We will define only one method, the initializer for the class, and just make it call the constructor of its super class. We'll divide this into two steps, first creating the code for the method, and then specifying the access to the method and arguments, etc.

(define init-code (make-code))
(jas-code-addinsn init-code
                  (aload_0))
(jas-code-addinsn init-code
                  (invokenonvirtual
                   (make-method-cpe "java/lang/Object" "<init>" "()V")))
(jas-code-addinsn init-code
                  (return))

make-code is a function that creates a new instance of a code-body. code-bodies are strictly non-pornographic and represent the body of a method. Once you create a code-body, you can append instructions to it with the jas-code-addinsn function, Other methods exist to add a catch table to the body to handle exceptions.

The second step in defining methods is to specify arguments and access control to the method.

(jas-class-addmethod my-class acc-public "<init>" "()V" init-code ())
The jas-class-addmethod function allows a code body to be added to a class. It takes a few parameters which describe the access permissions to the class, its name, signature and any exception attributes for the method. At this point, the code to generate the bytecode is ready to be generated, and the entire program looks like this.
;; script to create a class that does nothing.


                                        ; make-class-env creates
                                        ; a ClassEnv object which is
                                        ; used to store information about
                                        ; an object.
(define my-class (make-class-env))

                                        ; make-code creates a code object
                                        ; which contains the body of a
                                        ; method.

(define init-code (make-code))
(jas-code-addinsn init-code
                  (aload_0))
(jas-code-addinsn init-code
                  (invokenonvirtual
                   (make-method-cpe "java/lang/Object" "<init>" "()V")))
(jas-code-addinsn init-code
                  (return))

                                        ; fill up the class with goodies
(jas-class-setsuperclass my-class (make-class-cpe "java/lang/Object"))
(jas-class-setclass my-class (make-class-cpe "out"))
(jas-class-setaccess my-class acc-public)
(jas-class-addmethod my-class acc-public "<init>" "()V" init-code ())

                                        ; and write it all out
(jas-class-write my-class (make-outputstream "out.class"))
This example is provided with the distribution, and this sequence shows how to use the script driver to run this piece of code from the distribution root directory.
% java scm.driver examples/simple.jas
% ls -l out.class
-rw-r--r--  1 kbs           118 Dec 31 11:43 out.class
% javap -c -p out
public class out extends java.lang.Object {
    public out();
 
Method out()
   0 aload_0
   1 invokenonvirtual #7 <Method java.lang.Object.<init>()V>
   4 return
 
}
% javap -verify out
Class out succeeds
You should always run the verifier after generating bytecode. By default, the verifier is not run when loading files locally, so errors in the bytecode will cause the VM to die.
 
T o o l s/Assembler/Script examples

Simple | Loops | Mini arithmetic compiler

http://www.sbktech.org/jas-scm-simple.html Revised: Thu Dec 19 08:51:14 1996
Copyright (C) 1996 KB Sriram.
Comments, bug reports: kbs@sbktech.org
Found something useful?